package com.leakey.ddt;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.xquery.XQConnection;
import javax.xml.xquery.XQDataSource;
import javax.xml.xquery.XQException;
import javax.xml.xquery.XQExpression;
import javax.xml.xquery.XQSequence;

import net.sf.saxon.xqj.SaxonXQDataSource;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class GuideFactory {
	String path;
	XQDataSource dataSource;
	
	public GuideFactory(){
		String fileName = "E:/我的文档/workspace(Eclipse RCP)/com.leakey.ddt/src/com/leakey/ddt/system.xml";
		File f = new File(fileName);
		path = f.toURI().getPath();
		dataSource = new SaxonXQDataSource();
	}
	
	/**
	 * 获取合适的指引
	 * @param distance 距离
	 * @param wind 风力
	 * @return 合适的指引
	 * @throws IOException
	 * @throws XQException ;
	 */
	public List<Guide> getGuides(double distance,double wind) throws IOException, XQException{
		//生成所有组合的数据
//		BufferedReader br = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("system.xml")));
//		StringBuffer file = new StringBuffer();
//		String line="";
//		while((line = br.readLine()) != null){
//			file.append(line);
//		}
		ArrayList<Guide> guides = new ArrayList<Guide>();
		XQConnection conn = dataSource.getConnection();
		//条件为/ddt//category[dynamic/@distance=""]
//		String queryString = "for $guide in doc('"+path+"')/ddt//category[dynamic/@distance=\""+distance+"\"] return $guide";
		int roundDistance = (int)Arith.round(distance, 0);
		System.out.println("计算出的距离："+roundDistance);
		String queryString = "for $guide in doc('" + path + "')/ddt/category,"
		+ " $dynamic in $guide/dynamic[@distance='"+roundDistance+"']  order by $guide/@name" 
		+ " return <result name='{$guide/@name}' regulation-type='{$guide/@regulation-type}' regulation='{$guide/@regulation}' " +
				"angle='{$dynamic/@angle}' strength='{$dynamic/@strength}'></result>";
		
		XQExpression expression = conn.createExpression();
		XQSequence results = expression.executeQuery(queryString);
		while (results.next()) {
			NamedNodeMap attrs = results.getNode().getAttributes();
			Guide guide = new Guide();
			guide.setName(attrs.getNamedItem("name").getNodeValue());
			String regulationType = attrs.getNamedItem("regulation-type").getNodeValue();
			String regulation=attrs.getNamedItem("regulation").getNodeValue();
			String angle="";
			String dynamics="";
			if(Constants.CHANGE_ANGLE.equals(regulationType.trim())){
				String tmp = attrs.getNamedItem("angle").getNodeValue();
				if(tmp!=null && !"".equals(tmp.trim())){
					angle = ((int)Math.rint((Double.parseDouble(tmp)+wind*Integer.parseInt(regulation))))+"";
				}
				dynamics = attrs.getNamedItem("strength").getNodeValue();
			}else if(Constants.CHANGE_STRENGTH.equals(regulationType.trim())){
				String tmp = attrs.getNamedItem("strength").getNodeValue();
				if(tmp!=null && !"".equals(tmp.trim())){
					dynamics = ((int)Math.rint((Double.parseDouble(tmp)-wind*Integer.parseInt(regulation))))+"";
				}
				angle = attrs.getNamedItem("angle").getNodeValue();
			}
			if("".equals(angle)||"".equals(dynamics))continue;
			guide.setAngle(angle);
			guide.setDynamic(dynamics);
			guides.add(guide);
		}
		// 释放资源
		results.close();
		expression.close();
		conn.close();
		return guides;
	}
}
